/*** */

layui.define(['jquery', 'layer','table'], function (exports) { //提示：模块也可以依赖其它模块，如：layui.define('layer', callback);
    var $ = layui.$,
        t = layui.table,
        layer = layui.layer;
        layer.config({
            extend: 'skin/yourskin/style.css', //加载您的扩展样式
          });
    var obj = {
        api: 'http://localhost:8085/api/',
        /**
         * Promise 请求 --带Token
         * @param {接口} url 
         * @param {POST OR GET} methods 
         * @param {参数} params 
         */
        ajax(url, methods, params,type) {
            return new Promise((resolve, reject) => {
                $.ajax({
                    type: methods,
                    url: `${this.api}/${url}`,
                    contentType: type,
                    data: params,
                    beforeSend: function (request) {
                        request.setRequestHeader("Token",localStorage.getItem('Token'));
                    },
                    success: function (response) {
                        resolve(response);
                    },
                    error: function (error) {
                        let msg = error.responseJSON.ExceptionMessage;
                        obj.admin.topMsg(msg)
                        reject(error)
                    }
                });
            });
        },
        //根据QueryString参数名称获取值
        getQueryStringByName(name) {
            var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
            if (result == null || result.length < 1) {
                return "";
            }
            return result[1];
        },
        /**
         *获取guid
         *
         * @returns
         */
        guid() {
            return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = Math.random() * 16 | 0,
                    v = c == 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);
            });
        },
        like(obj){
            let k =[];
            for(var i in obj){
                if(i.indexOf('like')!=-1){
                    k.push(obj[i])
                    delete obj[i];
                }
            }           
            obj.X1 = k.toString();
            return obj;
        },
        //根据Quer
        admin: {
            /**
             *打开iframe层
             *
             * @param {url} url
             * @param {title} title
             * @param {type} type
             * @param {end} end
             */
            prop(url,title, type,end) {
                let area = [];
                switch (type) {
                    //默认层
                    case 1:
                        area = ["423px", '90%']
                    break;
                    default:
                        area = type
                        break;
                }
                layer.open({
                    type: 2,
                    area,
                    offset: 't',
                    fixed: true, //不固定
                    maxmin: true,
                    skin: 'layui-layer-demo',
                    shadeClose: true, //开启遮罩关闭
                    title,
                    content: url, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                    end,
                    success(layero,index){
                    }
                });
            },
            /**
             * 显示在顶部
             * @param {提示信息} msg 
             * @param {回掉} end 
             */
            topMsg(msg,end) {
                layer.msg(msg, {
                    offset: 't',
                    end,
                    time: 1000
                })
            },              
            /**
             * 禁止使用按钮
             * @param {选择器} select 
             */          
            disabled(select){
                $(select).addClass('layui-btn-disabled')
                $(select).prop('disabled',true)
            },
            /**
             *解除禁制状态
             *
             * @param {选择器} select
             */
            using(select){
                $(select).removeClass('layui-btn-disabled')
                $(select).prop('disabled',false)
            },
            /**
             *关闭当前iframe层
             *
             */
            killIframe(){
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
            },
            loading(){
                var msg = layer.msg('努力中加载中...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,offset: '0px',time: 0});
                return msg;
            },
            /**
             *询问框
             *
             * @param {*} msg
             * @param {*} end
             */
            confim(msg,end){
                layer.confirm(msg, {icon: 3, title:'提示'}, end);
            },
            auto(){
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.iframeAuto(index);
            },
            getCheck(table_id){
                var checkStatus = t.checkStatus(table_id)
                return checkStatus;
            },
            /**
             *表单填充
             *
             * @param {layui.form} form
             * @param {选择器} filter
             * @param {表单值} form_obj
             */
            set_form(form,filter,form_obj){
                form.val(filter,form_obj)
            }
        }
    };

    //输出api接对象
    exports('utilJs', obj);
}); 